﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Syndication;

namespace RssServiceLibrary
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "IRssFeedService" in both code and config file together.
    public class IRssFeedService : IIRssFeedService
    {
        public SyndicationFeedFormatter Rss(string format)
        {
            var articles = GetRecentArticles();
            var feed = new SyndicationFeed()
            {
                Title = new TextSyndicationContent("Web Rss Feed"),
                Description = new TextSyndicationContent("Recent web articles"),
                Items = from article in articles
                        select new SyndicationItem()
                        {
                            Title = new TextSyndicationContent(article.Title),
                            Content = new TextSyndicationContent(article.Abstract)
                        }
            };
            if (format.Equals("atom"))
                return new Atom10FeedFormatter(feed);
            else
                return new Rss20FeedFormatter(feed);
        }

        private static IEnumerable<Article> GetRecentArticles()
        {
            var articles = new List<Article>();
            for (var i = 0; i < 10; i++)
            {
                var article = new Article()
                {
                    Title = "Just another title " + i,
                    Abstract = "Mary had a little lamb, little lamb, little lamb!"
                };
                articles.Add(article);
            }
            return articles;
        }
    }

    public class Article
    {
        public string Title { get; set; }
        public string Abstract { get; set; }
    }
}
